﻿using System;
using System.Collections;
using System.Collections.ObjectModel;
using NHibernate;
using ParaEditor.Common;
using ParaEditor.Domain.Entities;

namespace ParaEditor.DAO.ParaDAO
{
    public class Para4002DAO : BaseDAO
    {
        private static Para4002 p4002 = new Para4002();
        public Para4002DAO()
            : base(p4002)
        {
        }

        public ObservableCollection<Para4002> GetObjectByInfoVer(int infoVer)
        {
            ObservableCollection<Para4002> list = new ObservableCollection<Para4002>();
            IList iList = base.GetObjectByInfoVer(infoVer);
            if (iList != null)
            {
                foreach (Para4002 item in iList)
                {
                    list.Add(item);
                }
            }
            return list;
        }

        public ResultValue UpdateTicketTypeInfoCnNamePointer(string nameCn, int pointer)
        {
            ResultValue rv = new ResultValue();
            string sqlQuery = "update TBL_METRO_TICKET_TYPE set NAME_CN_POINTER = " + pointer + " where trim(NAME_CN) = '" + nameCn + "'";
            try
            {
                ISQLQuery Query = Session.CreateSQLQuery(sqlQuery).AddEntity(p4002.GetType());
                Query.ExecuteUpdate();
                Session.Flush();
                Session.Clear();
                rv.Code = 0;
            }
            catch (Exception e)
            {
                rv.Code = -1;
                rv.Msg = e.Message;

            }
            return rv;
        }

        public ResultValue UpdateTicketTypeInfo(Para4002 param)
        {
            ResultValue rv = new ResultValue();
            string sqlQuery = "update TBL_METRO_TICKET_TYPE set LOST_FINE = " + param.lostFine + ",FAR_FINE=";
            sqlQuery += param.farFine + ",LATE_FINE=" + param.lateFine + ",IO_ADDON_FINE=" + param.ioAddonFine + ",INIT_VAL=";
            sqlQuery += param.initVal + ",TOMORROW_HOUR=" + param.tomorrowHour + ",SPACE_TIME=" + param.spaceTime + ",LONGEST_PERIOD_AREA_1= ";
            sqlQuery += param.longestPeriodArea1 + ",LONGEST_PERIOD_AREA_2=" + param.longestPeriodArea2 + ",LONGEST_PERIOD_AREA_3=";
            sqlQuery += param.longestPeriodArea3 + ",LONGEST_PERIOD_AREA_4=" + param.longestPeriodArea4 + ",LONGEST_PERIOD_AREA_5 = ";
            sqlQuery += param.longestPeriodArea5 + ",LONGEST_PERIOD_AREA_6=" + param.longestPeriodArea6 + ",LONGEST_PERIOD_AREA_7=" + param.longestPeriodArea7;
            sqlQuery += ",LONGEST_PERIOD_AREA_8=" + param.longestPeriodArea8 + ",LONGEST_PERIOD_AREA_9=" + param.longestPeriodArea9;
            sqlQuery += ",LONGEST_PERIOD_AREA_10=" + param.longestPeriodArea10 + ",LONGEST_PERIOD_AREA_11=" + param.longestPeriodArea11;
            sqlQuery += ",LONGEST_PERIOD_AREA_12=" + param.longestPeriodArea12 + ",LONGEST_PERIOD_AREA_13=" + param.longestPeriodArea13;
            sqlQuery += ",LONGEST_PERIOD_AREA_14=" + param.longestPeriodArea14 + ",LONGEST_PERIOD_AREA_15=" + param.longestPeriodArea15;
            sqlQuery += ",LONGEST_PERIOD_AREA_16=" + param.longestPeriodArea16 + ",LONGEST_PERIOD_AREA_17=" + param.longestPeriodArea17;
            sqlQuery += " where INFO_VER = " + param.para4002Pk.infoVer + " and TICKET_TYPE = " + param.para4002Pk.ticketType;
            try
            {
                ISQLQuery Query = Session.CreateSQLQuery(sqlQuery).AddEntity(p4002.GetType());
                Query.ExecuteUpdate();
                Session.Flush();
                Session.Clear();
                rv.Code = 0;
            }
            catch (Exception e)
            {
                rv.Code = -1;
                rv.Msg = e.Message;

            }
            return rv;
        }

        public ObservableCollection<Para4002> GetObjectByMaxInfoVer()
        {
            ObservableCollection<Para4002> list = new ObservableCollection<Para4002>();
            int infoVer = base.GetMaxID();
            IList iList = base.GetObjectByInfoVer(infoVer);
            foreach (Para4002 item in iList)
            {
                list.Add(item);
            }
            return list;
        }
    }
}
